﻿package mortal.game.view.wardrobe.view
{
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.wardrobe.*;
    import mortal.game.view.wardrobe.util.*;
    import mortal.mvc.core.*;

    public class WardrobeInfoCellRenderer extends GCellRenderer
    {
        private var _bg:GImageBitmap;
        private var _selectBg:ScaleBitmap;
        private var _fashionName:GTextFiled;
        private var _item:BaseItem;
        private var _changeShapeBtn:GLoadingButton;
        private var _cancelChangeBtn:GLoadingButton;
        private var _itemData:ItemData;
        private var _isSelect:Boolean;

        public function WardrobeInfoCellRenderer()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.gImageBitmap(ImagesConst.WardrobeItemBg, 0, 0, this);
            this._selectBg = UIFactory.bg(0, 0, 136 + 6, 136 + 6, this, ImagesConst.selectFilter);
            this._selectBg.visible = false;
            this._fashionName = UIFactory.gTextField("", 3, 7 - 2, 135, 25, this, GlobalStyle.textFormatZi.setSize(14).center());
            this._item = UICompomentPool.getUICompoment(BaseItem);
            this._item.setItemStyle(ItemStyleConst.Big, "", 0, 0);
            this._item.x = 37 + 4;
            this._item.y = 48 + 5;
            this._item.isDragAble = false;
            this.addChild(this._item);
            this._changeShapeBtn = UIFactory.gLoadingButton(ResFileConst.WardrobeDressBtn, (47 + 1), 109 + 7, 46, 23, this);
            this._cancelChangeBtn = UIFactory.gLoadingButton(ResFileConst.WardrobeGetOffBtn, (47 + 1), 109 + 7, 46, 23, this);
            this._cancelChangeBtn.visible = false;
            this._changeShapeBtn.configEventListener(MouseEvent.CLICK, this.onChangeHandler);
            this._cancelChangeBtn.configEventListener(MouseEvent.CLICK, this.onChangeHandler);
            this.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this.configEventListener(MouseEvent.MOUSE_OVER, this.onOverHandler);
            this.configEventListener(MouseEvent.MOUSE_OUT, this.onOutHandler);
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            Dispatcher.addEventListener(EventName.WardrobeSelDefaultFashion, this.onSelDefaultFashionHandler);
            NetDispatcher.addCmdListener(ServerCommand.FashionShapeChange, this.onFashionShapChange);
            NetDispatcher.addCmdListener(ServerCommand.PetSharpSuccess, this.onPetShapChange);
            NetDispatcher.addCmdListener(ServerCommand.PetSharpChangeCode, this.onPetShapChange);
            NetDispatcher.addCmdListener(ServerCommand.MountSharpSuccess, this.onMountShapChange);
            return;
        }// end function

        private function removeListeners() : void
        {
            Dispatcher.removeEventListener(EventName.WardrobeSelDefaultFashion, this.onSelDefaultFashionHandler);
            NetDispatcher.removeCmdListener(ServerCommand.FashionShapeChange, this.onFashionShapChange);
            NetDispatcher.removeCmdListener(ServerCommand.PetSharpSuccess, this.onPetShapChange);
            NetDispatcher.removeCmdListener(ServerCommand.PetSharpChangeCode, this.onPetShapChange);
            NetDispatcher.removeCmdListener(ServerCommand.MountSharpSuccess, this.onMountShapChange);
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            super.data = param1;
            this._itemData = param1 as ItemData;
            if (this._itemData)
            {
                this._item.itemData = this._itemData;
                this._fashionName.text = this._itemData.itemInfo.name;
                this.updateBtnState();
            }
            else
            {
                this._item.itemData = null;
                this._fashionName.text = "";
                this._changeShapeBtn.visible = false;
                this._cancelChangeBtn.visible = false;
            }
            return;
        }// end function

        override public function set selected(param1:Boolean) : void
        {
            super.selected = param1;
            if (param1)
            {
                EffectManager.glowFilterReg(this._bg, null, 0);
            }
            else
            {
                EffectManager.glowFilterUnReg(this._bg);
            }
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            if (this._itemData != WardrobeWin.currSelFashionData)
            {
                WardrobeWin.currSelFashionData = this._itemData;
                Dispatcher.dispatchEvent(new DataEvent(EventName.WardrobeSelFashion));
                this.selected = true;
            }
            return;
        }// end function

        private function onOverHandler(event:MouseEvent) : void
        {
            CursorManager.showCursor(CursorManager.BUTTON);
            return;
        }// end function

        private function onOutHandler(event:MouseEvent) : void
        {
            CursorManager.hideFromButton();
            return;
        }// end function

        private function onSelDefaultFashionHandler(event:DataEvent) : void
        {
            var _loc_2:int = 0;
            if (this._itemData)
            {
                _loc_2 = WardrobeUtil.getShapCodeByType(WardrobeWin.currSelWardrobe);
                if (_loc_2 > 100 && _loc_2 == this._itemData.itemCode)
                {
                    this.selected = true;
                    WardrobeWin.currSelFashionData = this._itemData;
                }
            }
            else
            {
                this.selected = false;
            }
            return;
        }// end function

        private function onChangeHandler(event:MouseEvent) : void
        {
            if (event.target == this._changeShapeBtn)
            {
                if (ItemsUtil.isPrescriOutTime(this._itemData))
                {
                    MsgManager.showRollTipsMsg(Language.getString(41404));
                    return;
                }
                switch(WardrobeWin.currSelWardrobe)
                {
                    case EAdvanceComponentType._EAdvanceComponentTypeClothes:
                    case EAdvanceComponentType._EAdvanceComponentTypeWing:
                    case EAdvanceComponentType._EAdvanceComponentTypeWeapon:
                    {
                        GameProxy.fashion.changeShape(this._itemData.itemCode, WardrobeWin.currSelWardrobe);
                        break;
                    }
                    case EAdvanceComponentType._EAdvanceComponentTypePet:
                    {
                        GameProxy.pet.changeSharp(this._itemData.itemCode);
                        break;
                    }
                    case EAdvanceComponentType._EAdvanceComponentTypeMount:
                    {
                        GameProxy.mount.mountIllusion(this._itemData.itemCode);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            if (event.target == this._cancelChangeBtn)
            {
                switch(WardrobeWin.currSelWardrobe)
                {
                    case EAdvanceComponentType._EAdvanceComponentTypeClothes:
                    case EAdvanceComponentType._EAdvanceComponentTypeWing:
                    case EAdvanceComponentType._EAdvanceComponentTypeWeapon:
                    {
                        GameProxy.fashion.changeShape(0, WardrobeWin.currSelWardrobe);
                        break;
                    }
                    case EAdvanceComponentType._EAdvanceComponentTypePet:
                    {
                        GameProxy.pet.changeSharp(0);
                        break;
                    }
                    case EAdvanceComponentType._EAdvanceComponentTypeMount:
                    {
                        GameProxy.mount.mountIllusion(0);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return;
        }// end function

        private function updateBtnState() : void
        {
            var _loc_1:* = WardrobeUtil.getShapCodeByType(WardrobeWin.currSelWardrobe);
            if (this._itemData && this._itemData.itemCode == _loc_1)
            {
                this._changeShapeBtn.visible = false;
                this._cancelChangeBtn.visible = true;
            }
            else
            {
                this._changeShapeBtn.visible = true;
                this._cancelChangeBtn.visible = false;
            }
            return;
        }// end function

        private function onFashionShapChange(param1:Object) : void
        {
            var _loc_2:* = param1 as int;
            if (this._itemData)
            {
                this.updateBtnState();
            }
            return;
        }// end function

        private function onPetShapChange(param1:Object) : void
        {
            var _loc_2:* = Cache.instance.newPet.pet.sharpId;
            if (this._itemData)
            {
                this.updateBtnState();
            }
            return;
        }// end function

        private function onMountShapChange(param1:Object) : void
        {
            var _loc_2:* = Cache.instance.mount.mountData.sPublicMount.sharpId;
            if (this._itemData)
            {
                this.updateBtnState();
            }
            return;
        }// end function

        private function clear() : void
        {
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            this._bg.dispose();
            this._bg = null;
            this._selectBg.dispose();
            this._selectBg = null;
            this._fashionName.dispose();
            this._fashionName = null;
            this._item.dispose();
            this._item = null;
            this._itemData = null;
            this._isSelect = false;
            this._changeShapeBtn.dispose();
            this._changeShapeBtn = null;
            this._cancelChangeBtn.dispose();
            this._cancelChangeBtn = null;
            this.removeListeners();
            return;
        }// end function

    }
}
